﻿using System;

namespace CaveArt.Common.Util
{
    public class NTLog : IDisposable
    {
        private string logName;
        private string eventName;
        private bool disposed = false;
        private System.Diagnostics.EventLog log;

        #region Properties
        ///<summary>The name of the service used in the service database.</summary>
        public string LogName { get { return this.logName; } }
        ///<summary>The name of the service that will be displayed in the services snap-in.</summary>
        public string EventName { get { return this.eventName; } }
        ///<summary>Tells if the service has been disposed of already.</summary>
        public bool Disposed { get { return this.disposed; } }
        #endregion

        #region Constructors
        public NTLog(string _logname, string _eventName)
        {
            this.logName = _logname;
            this.eventName = _eventName;
            
            // Create the source, if it does not already exist.
            try
            {
                if (!System.Diagnostics.EventLog.SourceExists(this.eventName))
                {
                    // An event log source should not be created and immediately used.
                    // There is a latency time to enable the source, it should be created
                    // prior to executing the application that uses the source.
                    System.Diagnostics.EventLog.CreateEventSource(this.eventName, this.logName);
                }
            }
#if DEBUG
            catch (System.Security.SecurityException e)
            {
                // (e.Message)
            }
#else
            catch {}
#endif
        }
        #endregion


        #region IDisposable Members
        public void Dispose()
        {
            if (!this.disposed)
            {
                this.disposed = true;
                if (this.log != null)
                {
                    this.log.Close();
                    this.log.Dispose();
                    this.log = null;
                }
            }
        }
        #endregion

        #region Helper Methods
        private void checkLog()
        {
            if (this.log == null)
            {
                this.log = new System.Diagnostics.EventLog(this.logName);
                this.log.Source = this.eventName;
            }
        }

        public void Log(string Message)
        {
            checkLog();
            this.log.WriteEntry(Message);
            this.log.Close();
        }

        public void Log(string Message, System.Diagnostics.EventLogEntryType EntryType)
        {
            checkLog();
            this.log.WriteEntry(Message, EntryType);
        }

        public void Log(string Message, System.Diagnostics.EventLogEntryType EntryType, int EventID)
        {
            checkLog();
            this.log.WriteEntry(Message, EntryType, EventID);
        }

        public void Log(string Message, System.Diagnostics.EventLogEntryType EntryType, short Category, int EventID)
        {
            checkLog();
            this.log.WriteEntry(Message, EntryType, EventID, Category);
        }
        #endregion

    }
}
